<?php

namespace app\agentapi\controller\user;

use app\agentapi\controller\Controller;
use think\Cache;
use app\admin\model\BaseSetting as SettingModel;
use think\Db;
use app\common\library\WsgtLog;

/**
 * 楼盘相关
 */
class Build extends Controller{
    private $config;
    public function _initialize(){
        //parent::_initialize();
        // 存储配置信息
        $this->config = SettingModel::getItem('storage');
    }

    /**
     * 渠道人员获取项目组跟楼盘二级联动数据
     */
    public function getSelectData(){
        $userInfo=Db::name('yonghui_user')->where(['id'=>$this->userId])->field('id,type,status,companyId,loupanids')->find();
        if($userInfo['type']<3){
            return json(['msg'=>'该用户无权限','code'=>'error','data'=>[]]);
        }
        if($userInfo['status']==2){
            return json(['msg'=>'该用户已被禁用','code'=>'error','data'=>[]]);
        }
        //获取项目组信息
        $flag=input('post.flag');
        $project=Db::name('yonghui_project')->where(['companyId'=>$userInfo['companyId']])->field('id,name')->select()->toArray();
        array_unshift($project,['id'=>0,'name'=>'全部']);
        if($flag=='select'){
            //获取楼盘信息
            foreach ($project as $k=>$v){
                if($v['id']==0){
                    $infos[]=['id'=>0,'name'=>'全部'];
                }else{
                    $infos=Db::name('yonghui_building')->where(['projectId'=>$v['id']])->field('id,name')->select();
                }

                $project[$k]['buildIngInfos']=$infos;
            }
        }
        return json(['msg'=>'获取成功','code'=>'success','data'=>$project]);
    }
    /**
     * 获取房源列表
     */
    public function getBuildList(){
        //根据当前登录用户类型 以及所属公司获取可以选择的楼盘列表
        $userInfo=Db::name('yonghui_user')->where(['id'=>$this->userId])->field('id,type,status,companyId,loupanids')->find();
        if($userInfo['status']==2){
            return json(['msg'=>'该用户已被禁用','code'=>'error','data'=>[]]);
        }
        //获取该登陆者所属自营公司 添加的未禁用的楼盘列表
        $com=Db::name('yonghui_company')->where(['id'=>$userInfo['companyId']])->find();
        if($userInfo['type']==1||$userInfo['type']==2){
            //驻场或者置业顾问
            if($userInfo['loupanids']=='all'){

            }else{
                $where['id']=['=',$userInfo['loupanids']];
            }
        }
        if($com['upId']==0){
            //登录人为置业顾问，
            $cid=$userInfo['companyId'];
        }else{
            //登录人为渠道销售人员
            $cid=$com['upId'];
        }
        if(!empty($this->postData()['search'])){
            $where['name']=['like','%'.$this->postData()['search'].'%'];
            $where['status']=['=',1];
            $where['companyId']=['=',$cid];
        }else{
            $where['status']=['=',1];
            $where['companyId']=['=',$cid];
        }
        if($userInfo['type']>2){
            $pro=input('post.projectId');
            if($pro){
                $where['projectId']=['=',$pro];
            }
        }
        $buildList=Db::name('yonghui_building')->where($where)->field('name,detail,id,bayArea,avgprice,loupanimageids')->paginate(10000, false, [
            'query' => request()->request()
        ]);
        $buildList=$buildList->toArray();
        if($buildList){
            foreach($buildList['data'] as $k=>$v){
                $a=json_decode($v['loupanimageids'],'true');
                $listi=Db::name('upload_file')->where(['file_id'=>$a['0']])->field('file_id,file_url,file_name')->find();
                $buildList['data'][$k]['imageUrl']=$listi['file_url'].'/'.$listi['file_name'];
            }
        }
        return json(['msg'=>'获取成功','code'=>'success','data'=>$buildList]);
    }
    /**
     * 获取房源详情
     */
    public function getBuildInfo(){
        $log = WsgtLog::instance();
        $log->setFilename('loupan/info');
        $log->info($this->postData(), '传参信息');
        $u_bIds=input('post.user_id');
        $arr=explode('_',$u_bIds);
        $buildId=$arr[1];
        if(empty($buildId)){
            return json(['msg'=>'楼盘id不能为空','code'=>'error','data'=>[]]);
        }
        //判断该房源状态
        $buildInfo=Db::name('yonghui_building')->where(['id'=>$buildId])->find();
        if(empty($buildInfo)){
            return json(['msg'=>'未获取到楼盘信息','code'=>'error','data'=>[]]);
        }
        if($buildInfo['status']==2){
            return json(['msg'=>'该楼盘已下架','code'=>'error','data'=>[]]);
        }
        //获取首页轮播，楼盘轮播，沙盘轮播，配套图片，售楼处图片
        $buildInfo['bannerids']=$this->getImageUrl(json_decode($buildInfo['bannerids'],'true'));
        $buildInfo['loupanimageids']=$this->getImageUrl(json_decode($buildInfo['loupanimageids'],'true'));
        $buildInfo['shapanimageids']=$this->getImageUrl(json_decode($buildInfo['shapanimageids'],'true'));
        $buildInfo['peitaoimageids']=$this->getImageUrl(json_decode($buildInfo['peitaoimageids'],'true'));
        $buildInfo['slimageids']=$this->getImageUrl(json_decode($buildInfo['slimageids'],'true'));
        $buildInfo['characteristic']=explode('@',$buildInfo['characteristic']);
        $buildInfo['qita']=explode('@',$buildInfo['qita']);
        //楼盘详情用
        //$buildInfo['miaoshu']=htmlspecialchars_decode($buildInfo['miaoshu']);
        //$buildInfo['miaoshu']=str_replace("<img ", "<img style='width:100%;height:auto;'", $buildInfo['miaoshu']);
        //$buildInfo['buildingDetail']=htmlspecialchars_decode($buildInfo['buildingDetail']);
        //$buildInfo['buildingDetail']= str_replace("<img ", "<img style='width:100%;height:auto;'", $buildInfo['buildingDetail']);
        //获取动态信息
        $dongtaiInfo=Db::name('yonghui_building_dynamic')->where(['loupanid'=>$buildId])->order('createTime desc')->select()->toArray();
        if($dongtaiInfo){
            foreach($dongtaiInfo as $k=>$v){
                $userName=Db::name('yonghui_adminuser')->where(['id'=>$v['userId']])->value('name');
                $dongtaiInfo[$k]['userName']=$userName;
                $dongtaiInfo[$k]['createTime']=date('Y-m-d H:i:s',$v['createTime']);
            }
        }
        $buildInfo['dongtaiInfo']=$dongtaiInfo;
        //分享人信息// 渠道经纪人显示驻场联系方式，渠道经理联系方式联系方式//其他类型人员登录，显示驻场联系方式，楼盘录入人联系方式
        if(empty($arr[0])){
            $id=$this->userId;
        }else{
            $id=$arr[0];
        }
        //不是分享出去的，根据登录人类型获取联系方式
        $userType=Db::name('yonghui_user')->where(['id'=>$id])->find();
        if($userType['type']==3){
            //渠道经纪人,获取渠道经理联系方式
            $comInfo=Db::name('yonghui_company')->where(['id'=>$userType['companyId']])->value('userId');
            if($comInfo){
                $qudaoInfo=Db::name('yonghui_user')->where(['id'=>$comInfo])->find();
            }else{
                $qudaoInfo=[];
            }
            $buildInfo['lururenInfo']=$qudaoInfo;
        }else{
            //获取楼盘录入人联系方式
            $lururenInfo=Db::name('yonghui_adminuser')->where(['companyId'=>$buildInfo['companyId']])->find();
            $buildInfo['lururenInfo']=$lururenInfo;
        }
        //获取驻场联系方式
        $zhuchangInfo=Db::name('yonghui_user')->where(['type'=>1,'projectId'=>$buildInfo['projectId'],'loupanids'=>$buildId])->find();
        $buildInfo['zhuchangInfo']=$zhuchangInfo;
        $userInfo=Db::name('yonghui_user')->where(['id'=>$id])->field('password',true)->find();
        $useriMage=Db::name('upload_file')->where(['file_id'=>$userInfo['imageId']])->find();
        $userInfo['img']=$useriMage['file_url'].'/'.$useriMage['file_name'];
        $buildInfo['userInfo']=$userInfo;
        return json(['msg'=>'获取成功','code'=>'success','data'=>$buildInfo]);

    }
    /**
     * 获取图片链接
     */
    private function getImageUrl($ids){
        $imageInfos=Db::name('upload_file')->where('file_id','in',$ids)->select();
        $arr=[];
        if($imageInfos){
            foreach($imageInfos as $k=>$v){
                $arr[]=$v['file_url'].'/'.$v['file_name'];
            }
        }
        return $arr;
    }

    /**
     * 获取资讯列表
     */
    public function getzixunlist(){
        $type=input('post.type');
        if(empty($type)){
            return json(['msg'=>'资讯类型参数不能为空','code'=>'error','data'=>[]]);
        }
        $where['type']=['=',$type];
        $list=Db::name('yonghui_zixun')->where($where)->order('createTime desc')->select()->toArray();
        if($list){
            foreach ($list as $k=>$v){
                $list[$k]['content']=htmlspecialchars_decode($v['content']);
                $list[$k]['createTime']=date('Y-m-d H:i:s',$v['createTime']);
                $list[$k]['imgUrl']="https://yh.globalbt.cn/".$this->get_between($list[$k]['content'],"https://yh.globalbt.cn/",'?x-oss-process=style/a_v');
            }
            return json(['msg'=>'获取成功','code'=>'success','data'=>$list]);
        }
        return json(['msg'=>'未获取到信息','code'=>'success','data'=>[]]);
    }

    /**
     *   截取字符串
     */
    private function get_between($input, $start, $end) {
        $substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));
        return $substr;
    }
    /**
     * 获取资讯详情
     */
    public function getzixuninfo(){
        $id=input('post.id');
        if(empty($id)){
            return json(['msg'=>'id参数不能为空','code'=>'error','data'=>[]]);
        }
        $list=Db::name('yonghui_zixun')->where(['id'=>$id])->find();
        $list['content']=htmlspecialchars_decode($list['content']);
        $list['content']=str_replace("<img ", "<img style='width:100%;height:auto;'", $list['content']);
        $list['createTime']=date('Y-m-d H:i:s',$list['createTime']);
        if($list){
            return json(['msg'=>'获取成功','code'=>'success','data'=>$list]);
        }
        return json(['msg'=>'未获取到信息','code'=>'success','data'=>[]]);
    }
}
